import { createRouter, createWebHistory } from 'vue-router'


const Home = ()=>import('../components/Home.vue')
const Login = ()=>import('../components/Login.vue')
const SigmUp = ()=>import('../components/SignUp.vue')

//注册路由
const routes = [
    {path: '/', name: 'home', component: Home},
    {path: '/login', name: 'login', component: Login},
    {path: '/signup', name: 'signup', component:SigmUp}
];
const router = createRouter({
    history: createWebHistory(),
    routes
});
router.beforeEach((to,from,next)=>{
    let token = localStorage.getItem('token');
    if (!token) {
        if (to.path == '/login' || to.path == '/signup') {
            next();
        } else {
            next('/login');
        }
    } else {
        if (to.path !== '/') {
            next('/');
        } else {
            next();
        }
    }
});
export default router;

